package main
import (
	"fmt"
	"net"
	"time"
)

func main(){
	
	/*自动连接服务器*/
	var tcp, ip, duankou string
	var by int
	fmt.Println("1   默认参数发送数据（tcp 127.0.0.1:8000）")
	fmt.Println("2   指定参数发送数据（手动输入协议地址端口）")
	fmt.Printf("请输入指令：")
	fmt.Scan(&by)
	if by == 1{
		tcp = "tcp"
		ip = "127.0.0.1"
		duankou = "8000"
	}else if by == 2{
		fmt.Printf("请输入协议：")
		fmt.Scan(&tcp)
		fmt.Printf("请输入IP地址：")
		fmt.Scan(&ip)
		fmt.Printf("请输入端口号：")
		fmt.Scan( &duankou)
	}else{
		fmt.Printf("输入有误，执行默认参数")
		tcp = "tcp"
		ip = "127.0.0.1"
		duankou = "8000"
	}
	dizhi := fmt.Sprintf("%s:%s", ip, duankou)
	conn, err := net.Dial(tcp, dizhi)
	if err != nil{
		fmt.Println("Dial er ： ", err)
		return
	}
	defer conn.Close()
	var str string
	fmt.Printf("请输入要发送的数据：")
	fmt.Scan(&str)
	num, err1 := conn.Write([]byte(str))
	if err1 != nil{
		fmt.Println("发送失败，错误 ：", err1)
		time.Sleep(time.Second*5)
		return
	}
	fmt.Println("发送成功，共", num, "个字节")
	time.Sleep(time.Second*5)
}